package com.keldian.mobilesafe.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Administrator on 15-12-14.
 */
public abstract class BaseSetupActivity extends Activity {

    private GestureDetector detector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        detector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                                   float velocityY) {

                if (Math.abs(e1.getRawY() - e2.getRawY()) > 100) {
                    Toast.makeText(getApplicationContext(), "不能这样滑动", Toast.LENGTH_SHORT).show();
                    return true;
                }

                if (Math.abs(velocityX) < 200 || Math.abs(velocityY) < 200) {
                    Toast.makeText(getApplicationContext(), "滑动得太慢了", Toast.LENGTH_SHORT).show();
                    return true;
                }


                if ((e2.getRawX() - e1.getRawX()) > 200) {
                    //显示上一个页面：从左往右滑动
                    System.out.println("显示上一个页面：从左往右滑动");
                    showPre();
                    return true;

                }

                if ((e1.getRawX() - e2.getRawX()) > 200) {
                    //显示下一个页面：从右往左滑动
                    System.out.println("显示下一个页面：从右往左滑动");
                    showNext();
                    return true;
                }


                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });
    }

    public abstract void showNext();

    public abstract void showPre();

    public void next(View view) {
        showNext();

    }

    /**
     * 上一步
     *
     * @param view
     */
    public void pre(View view) {
        showPre();

    }

    //3.使用手势识别器
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        detector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
}
